﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace randomTask
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int num1, num2, result,score=0;
        int sec = 0;
        string oper;
        Random ro1 = new Random();
        

        //点击开始
        private void start_btn_Click(object sender, EventArgs e)
        {
            randomNum();//随机出题;
            this.timer1.Enabled = true;
            try
            {
                Convert.ToInt32(lab_time.Text);
            }
            catch
            {
                MessageBox.Show("未输入所需时间");
            }
            sec = Convert.ToInt32(lab_time.Text);
            timer1.Interval = 1000;
            res_tb.Enabled = true;
            next_btn.Enabled = true;
            btn_finsh.Enabled = true;
            score_lab.Text = "0";
            start_btn.Enabled = false;
        }

        private int cal()
        {
            switch (oper)
            {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;

            }
            return result;
        }

        //做对返回true，做错返回false
        private bool check()
        {
            bool flag = false;
            try
            {
                int n = Convert.ToInt32(res_tb.Text);

            }
            catch
            {
                
                MessageBox.Show("未输入结果");
            }
            if (Convert.ToInt32(res_tb.Text) == result)
            {
                flag = true;
            }
            return flag;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            sec -= 1;
            lab_time.Text = sec.ToString();
            if (sec == 0)
            {
                timer1.Stop();
                timer1.Enabled = false;
                sec = 30;
                lab_time.Text = sec.ToString();
            }

        }

        private void lab_time_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //点击下一题
        private void next_btn_Click(object sender, EventArgs e)
        {
            res_tb.Enabled = true;
            res_tb.Focus();
            lab_time.Enabled = true;
            cal();
            if (check())//成功
            {
                label4.Text = "正确";
                label5.Text = "答案是：" + cal();
                score += 1;
                score_lab.Text = score.ToString();
            }
            else
            {
                label4.Text = "错误";
                label5.Text = "答案是："+num1+oper+num2+'='+ cal();

            }
            randomNum();
            res_tb.Text = null;
            res_tb.Enabled = true;
        }


        //随机出题
        private void randomNum()
        {
            num1 = ro1.Next(10, 100);
            num2 = ro1.Next(10, 100);
            lnum.Text = num1.ToString();
            rnum.Text = num2.ToString();
            int i = ro1.Next(2);
            switch (i)
            {
                case 0:
                    oper = "+";
                    op.Text = oper;
                    break;
                case 1:
                    oper = "-";
                    op.Text = oper;
                    break;
               
            }
        }


        private void btn_finsh_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            res_tb.Text = null;
            timer1.Stop();
            int used = 30 - sec;
            MessageBox.Show("本次得分：" + score.ToString() + "\n" + "用时" + used + "秒");
            sec = 30;           
            lab_time.Text = sec.ToString();
            score = 0;
            score_lab.Text = score.ToString();
            btn_finsh.Enabled = false;
            start_btn.Enabled = true;
        }
    }
}
